package cn.dansj.common.mail.utils;

import cn.dansj.common.utils.transfer.AssertUtils;
import cn.dansj.common.utils.transfer.ObjectUtils;
import cn.dansj.common.utils.transfer.StringUtils;

import java.io.Serializable;
import java.util.Date;

public class SimpleMailMessage implements MailMessage, Serializable {
    private String from;

    private String replyTo;

    private String[] to;

    private String[] cc;

    private String[] bcc;

    private Date sentDate;

    private String subject;

    private String text;

    public SimpleMailMessage() {
    }

    public SimpleMailMessage(SimpleMailMessage original) {
        AssertUtils.notNull(original, "'original' message argument must not be null");
        this.from = original.getFrom();
        this.replyTo = original.getReplyTo();
        this.to = copyOrNull(original.getTo());
        this.cc = copyOrNull(original.getCc());
        this.bcc = copyOrNull(original.getBcc());
        this.sentDate = original.getSentDate();
        this.subject = original.getSubject();
        this.text = original.getText();
    }


    @Override
    public void setFrom(String from) {
        this.from = from;
    }

    public String getFrom() {
        return this.from;
    }

    @Override
    public void setReplyTo(String replyTo) {
        this.replyTo = replyTo;
    }

    public String getReplyTo() {
        return this.replyTo;
    }

    @Override
    public void setTo(String to) {
        this.to = new String[]{to};
    }

    @Override
    public void setTo(String... to) {
        this.to = to;
    }

    public String[] getTo() {
        return this.to;
    }

    @Override
    public void setCc(String cc) {
        this.cc = new String[]{cc};
    }

    @Override
    public void setCc(String... cc) {
        this.cc = cc;
    }

    public String[] getCc() {
        return this.cc;
    }

    @Override
    public void setBcc(String bcc) {
        this.bcc = new String[]{bcc};
    }

    @Override
    public void setBcc(String... bcc) {
        this.bcc = bcc;
    }

    public String[] getBcc() {
        return this.bcc;
    }

    @Override
    public void setSentDate(Date sentDate) {
        this.sentDate = sentDate;
    }

    public Date getSentDate() {
        return this.sentDate;
    }

    @Override
    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubject() {
        return this.subject;
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public void copyTo(MailMessage target) {
        AssertUtils.notNull(target, "'target' MailMessage must not be null");
        if (getFrom() != null) {
            target.setFrom(getFrom());
        }
        if (getReplyTo() != null) {
            target.setReplyTo(getReplyTo());
        }
        if (getTo() != null) {
            target.setTo(copy(getTo()));
        }
        if (getCc() != null) {
            target.setCc(copy(getCc()));
        }
        if (getBcc() != null) {
            target.setBcc(copy(getBcc()));
        }
        if (getSentDate() != null) {
            target.setSentDate(getSentDate());
        }
        if (getSubject() != null) {
            target.setSubject(getSubject());
        }
        if (getText() != null) {
            target.setText(getText());
        }
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }

        if (other instanceof SimpleMailMessage) {
            SimpleMailMessage that = (SimpleMailMessage) other;
            return ObjectUtils.nullSafeEquals(this.from, that.from) &&
                    ObjectUtils.nullSafeEquals(this.replyTo, that.replyTo) &&
                    ObjectUtils.nullSafeEquals(this.to, that.to) &&
                    ObjectUtils.nullSafeEquals(this.cc, that.cc) &&
                    ObjectUtils.nullSafeEquals(this.bcc, that.bcc) &&
                    ObjectUtils.nullSafeEquals(this.sentDate, that.sentDate) &&
                    ObjectUtils.nullSafeEquals(this.subject, that.subject) &&
                    ObjectUtils.nullSafeEquals(this.text, that.text);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return ObjectUtils.nullSafeHash(this.from, this.replyTo, this.to, this.cc, this.bcc, this.sentDate, this.subject);
    }

    @Override
    public String toString() {
        return "SimpleMailMessage: " + "from=" + this.from + "; " +
                "replyTo=" + this.replyTo + "; " +
                "to=" + StringUtils.arrayToCommaDelimitedString(this.to) + "; " +
                "cc=" + StringUtils.arrayToCommaDelimitedString(this.cc) + "; " +
                "bcc=" + StringUtils.arrayToCommaDelimitedString(this.bcc) + "; " +
                "sentDate=" + this.sentDate + "; " +
                "subject=" + this.subject + "; " +
                "text=" + this.text;
    }

    private static String[] copyOrNull(String[] state) {
        if (state == null) {
            return null;
        }
        return copy(state);
    }

    private static String[] copy(String[] state) {
        return state.clone();
    }
}
